package com.yan.dom4J;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import java.io.*;

/**
 * @author taoge
 * @Date 2024/8/13 11:43
 * @Description
 */
public class Dom4JDemo2 {
    public static void main(String[] args) throws IOException {
        Document document = DocumentHelper.createDocument();
        Element books = document.addElement("books");
        Element book = books.addElement("book");
        Element id1 = book.addAttribute("id", "001");
        Element name = id1.addElement("name");
        name.addText("qqq");
        Element auther = id1.addElement("auther");
        auther.addText("yft");
        Element price = id1.addElement("price");
        price.addText("19.9");

        Element book1 = books.addElement("book");
        book1.addAttribute("id" , "002");
        book1.addElement("name").addText("aaa");
        book1.addElement("auther").addText("hpy");
        book1.addElement("price").addText("29.9");

        Element book2 = books.addElement("book");
        book2.addAttribute("id" , "003");
        book2.addElement("name").addText("zzz");
        book2.addElement("auther").addText("wl");
        book2.addElement("price").addText("39.9");

        OutputFormat prettyPrint = new OutputFormat().createPrettyPrint();
        prettyPrint.setEncoding("UTF-8");
        XMLWriter xmlWriter = new XMLWriter(new FileOutputStream("day18_code/src/com/yan/dom4J/newBooks.xml") , prettyPrint);
        xmlWriter.write(document);
        xmlWriter.close();

    }
}
